package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 登录并判断用户状态
     * @param employee
     * @param session
     * @return
     */
    @PostMapping("login")
    public R login(@RequestBody Employee employee, HttpSession session){
        String username = employee.getUsername();
        String password = employee.getPassword();
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        Employee e = employeeService.getOne(wrapper);
        password=DigestUtils.md5DigestAsHex(password.getBytes());
        if (e==null){
            return R.error("用户不存在!");
        }
        if (!e.getPassword().equals(password)){
            return R.error("密码错误!");
        }
        if (e.getStatus()==0){
            return R.error("账号被封!");
        }
        session.setAttribute("employee",e.getId());
        return R.success(e);
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @PostMapping("logout")
    public R<String> logout(HttpSession session){
        session.invalidate();
        return R.success("退出成功!");
    }

    /**
     * 添加员工信息
     * @param employee
     * @param session
     * @return
     */
    @PostMapping()
    public R<String> save(@RequestBody Employee employee, HttpSession session){
        log.info("新增员工，员工信息：{}",employee.toString());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        /*employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long e=(Long)session.getAttribute("employee");
        employee.setCreateUser(e);
        employee.setUpdateUser(e);*/
        employeeService.save(employee);
        return R.success("添加员工成功");
    }

    /**
     * 分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<IPage<Employee>> page(Integer page, Integer pageSize, String name){
        Page<Employee> pageInfo = new Page<>(page,pageSize);
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name),"name",name);
        wrapper.orderByDesc("update_time");
        employeeService.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }

    /**
     * 更新员工信息
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee){
        /*employee.setUpdateTime(LocalDateTime.now());
        Long empId = (Long)session.getAttribute("employee");
        employee.setUpdateUser(empId);*/
        employeeService.updateById(employee);
        return R.success("员工信息更改成功");
    }

    /**
     * 根据
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee byId = employeeService.getById(id);
        return R.success(byId);
    }

}
